"""
@author: xxw
@file: 04.用类去实现装饰器.py
@time: 2023/6/12 16:40
"""
import time

user = {"root": "123456", "admin": "admin123"}

class A():
    def __init__(self, username, passwd):
        self.username = username
        self.passwd = passwd

    def __call__(self, func):
        def inner(*args, **kwargs):
            if self.username in user and self.passwd == user.get(self.username):
                if self.username == "root":
                    print(f"装饰器带参数:{self.username}")
                    start = time.time()
                    result = func(*args, **kwargs)
                    end = time.time()
                    print(f"{func.__name__}执行花了{end - start}s")
                    return result
                else:
                    print("只有root用户具有权限")
            else:
                print("用户名或密码错误")
        return inner

@A(username="root",passwd="123456")     #a = A(username="root"),func1 = a(func1)
def add(a, b):
    time.sleep(1)
    return a+b
print(add(3,4))


# @A     # func1 = A(func1)
# def func1():
#     print("i am func1")
#     time.sleep(1)
#     print("i am func1")
#
# func1()